package cn.edu.sdjzu.xg23.kcsj01_backend.service.impl;

import cn.edu.sdjzu.xg23.kcsj01_backend.entity.EmployeeAssInstruction;
import cn.edu.sdjzu.xg23.kcsj01_backend.entity.Instruction;
import cn.edu.sdjzu.xg23.kcsj01_backend.mapper.AbsAchievementMapper;
import cn.edu.sdjzu.xg23.kcsj01_backend.mapper.EmployeeAssInstructionMapper;
import cn.edu.sdjzu.xg23.kcsj01_backend.mapper.InstructionMapper;
import cn.edu.sdjzu.xg23.kcsj01_backend.utils.SnowflakeIdGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class InstructionServiceImpl {

    @Autowired
    private InstructionMapper instructionMapper;

    @Autowired
    private AbsAchievementMapper absAchievementMapper;

    @Autowired
    private EmployeeAssInstructionMapper employeeAssInstructionMapper;

    @Autowired
    private SnowflakeIdGenerator idGenerator;

    @Transactional
    public boolean addInstruction(Instruction instruction) {
        Long sharedId = idGenerator.generateId();

        // 1. 插入Achievement记录
        absAchievementMapper.addAchievement(sharedId, "批示");

        // 2. 插入批示记录
        instruction.setId(sharedId);
        boolean success = instructionMapper.addInstruction(instruction);
        boolean insertSuccess =false;

        // 3. 处理申报人
        if (success && instruction.getApplicants() != null && !instruction.getApplicants().isEmpty()) {
            for (EmployeeAssInstruction applicant : instruction.getApplicants()) {
                applicant.setInstruction(new Instruction(sharedId));
            }
            insertSuccess = employeeAssInstructionMapper.insertApplicants(instruction.getApplicants());
        }
        return success && insertSuccess;
    }

    public boolean deleteInstruction(Long id) {
        // 1. 删除申报人关联记录
        employeeAssInstructionMapper.deleteByInstructionId(id);

        // 2. 删除支撑材料关联记录（如果有）
        // 这里需要添加支撑材料的删除逻辑，如果有相关Mapper

        // 3. 删除批示记录
        // instructionMapper.delete(id);


        return instructionMapper.delete(id);
    }

    public boolean updateInstruction(Instruction instruction) {
        // 1. 更新批示基本信息
        boolean updateSuccess = instructionMapper.updateInstruction(instruction);

        if (updateSuccess && instruction.getApplicants() != null) {
            // 2. 删除原有申报人
            employeeAssInstructionMapper.deleteByInstructionId(instruction.getId());

            // 3. 插入新申报人
            for (EmployeeAssInstruction applicant : instruction.getApplicants()) {
                applicant.setInstruction(new Instruction(instruction.getId()));
            }
            return employeeAssInstructionMapper.insertApplicants(instruction.getApplicants());
        }
        return updateSuccess ;
    }

    public PageInfo<Instruction> selectInstructionsByCondition(Integer pageNum, Integer pageSize, Instruction instruction, Long employeeId, String employeeName) {
        PageHelper.startPage(pageNum, pageSize);
        List<Instruction> instructionList = instructionMapper.selectInstructionsByCondition(instruction,  employeeId, employeeName);
        return new PageInfo<>(instructionList);
    }

    public boolean approveInstruction(Instruction instruction) {
        // 直接更新审批状态
        return instructionMapper.approvalInstruction(instruction);

    }
}